<?php

/**
 * This Class parse the variables in one array
 *
 * @package Dev-Tpl
 * @author Goncalo Margalho <gsky89@gmail.com>
 * @copyright Dev-House.Com (C) 2006-2008
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License
 * @version 1.0
 */
class Template {

    /**
     * Content of all Variables
     *
     * @var array
     */
    private $variables;

    /**
     * Directory of Template (Skins)
     *
     * @var string
     */
    private $tpl_dir;

    /**
     * Set directory of Template
     *
     * @param string $template_dir Directory of Template
     */
    public function __construct($template_dir){
        global $tpl_dir;
        $this->tpl_dir = $template_dir;
    }

    /**
     * Assign variable to a template
     *
     * @param string $variable_name Name of variable in template
     * @param mixed $value Value of variable assigned
     */
    public function assign($variable_name, $value){
        global $variables;
        $this->variables[ $variable_name ] = $value;
    }

    /**
	 * Delete old compiled file and compile a new file
	 *
	 * @param  string $template_dir Directory of template
	 * @param  string $tpl_name Name of template
	 */
    public static function compileTPL($template_dir, $tpl_name){
		if($file = glob( $template_dir . '/Compiled/' . $tpl_name . '*.php' ))
			foreach( $file as $delfile)
				unlink($delfile);

		$tpl = file_get_contents($template_dir . '/' . $tpl_name);
		$compiling = preg_replace('/{\$(.[^}]*?)\.(.*?)}/', '<?php echo $\\1[\'\\2\'];?>',$tpl);
        $compiling = preg_replace('/{\_(.[^}]*?)\.(.*?)}/', '<?php echo $var[\'\\1\'][\'\\2\'];?>',$compiling);
		$compiling = preg_replace('/{lang\.(.*?)}/', '<?php echo $lang[\'\\1\'];?>',$compiling);
		$compiling = preg_replace('/{user\.(.*?)}/', '<?php echo $user->getValues(\'\\1\');?>',$compiling);
		$compiling = preg_replace('/{uservar\.(.*?)}/', '$user->getValues(\'\\1\')',$compiling);
		$compiling = preg_replace('/{Tools\:\:(.*?)\.(.*?)}/', '<?php echo Tools::\\1(\\2);?>',$compiling);
		$compiling = preg_replace('/{\$key\.(.*?)}/', '<?php echo $key[\'\\1\'];?>',$compiling);
		$compiling = preg_replace('/{date\.\$(.*?)\.(.*?)}/', '<?php echo date(\'d-m-Y H:i\',$\\1[\'\\2\']);?>',$compiling);
		$compiling = preg_replace('/{editor\.(.*?)\.(.*?)}/', '<?php include_once \'../classes/editor.class.php\'; if(isset($editor)) $editor->add(\\1,\\2); else $editor = new editor(\\1,\\2);?>',$compiling);
        $compiling = preg_replace('/{editor\.(.*?)}/', '<?php include_once \'../classes/editor.class.php\'; if(isset($editor)) $editor->add(\\1); else $editor = new editor(\\1);?>',$compiling);
        $compiling = preg_replace('/{editor}/', '<?php $editor->insert();?>',$compiling);
        $compiling = preg_replace('/{date\.time}/', '<?php echo date(\'d-m-Y H:i\',time());?>',$compiling);
		$compiling = preg_replace('/{date\.(.*?)}/', '<?php echo date(\'d-m-Y H:i\',$var[\'\\1\']);?>',$compiling);
		$compiling = preg_replace('/{acplink\.(.*?)}/', '<?php echo ($qgeneral[\'acpajax\'] == 1) ? \'javascript:loadPage(\'\\1&ajax=1\')\' : \'\\1\'; ?>',$compiling);
		$compiling = preg_replace('/\[\$(.*?)\.(.*?)\]/', '$\\1[\'\\2\']',$compiling);
		$compiling = preg_replace('/\[\$key\.(.*?)\]/', '$key[\'\\1\']',$compiling);
		$compiling = preg_replace('/\[\$value\]/', '$value',$compiling);
		$compiling = str_replace('{$value}', '<?php echo $value;?>',$compiling);
		$compiling = str_replace('{$key}', '<?php echo $key;?>',$compiling);
		$compiling = preg_replace('/{\$(.*?)}/', '<?php echo $var[\'\\1\'];?>',$compiling);
		$compiling = preg_replace('/{lang\.(.*?)}/', '<?php echo $lang[\'\\1\'];?>',$compiling);
		$compiling = preg_replace('/\[\$(.*?)\]/', '$var[\'\\1\']',$compiling);
		$compiling = preg_replace('/(?:{if(?:\s+)condition="(.*?)"})/', '<?php if(\\1){ ?>',$compiling);
		$compiling = preg_replace('/(?:{elseif(?:\s+)condition="(.*?)"})/', '<?php } else if(\\1){ ?>',$compiling);
		$compiling = str_replace('{else}', '<?php } else{ ?>',$compiling);
		$compiling = str_replace('{/if}', '<?php } ?>',$compiling);
		$compiling = preg_replace('/(?:{loop(?:\s+)name="(.*?)"})/', '<?php $counter_\\1=0; foreach($var[\'\\1\'] as $key => $\\1){ $counter_\\1++; ?>',$compiling);
		$compiling = str_replace('{/loop}', '<?php } ?>',$compiling);
		fwrite( fopen( $template_dir . '/Compiled/' . $tpl_name . '.php', 'w' ), $compiling );
	}

    /**
     * Compile template, HTML to PHP
     *
     * @param string $tpl_name Name of file to compile
     * @param string $ext Extension of file to compile
     */
    public function burn($tpl_name, $ext){
        global $lang, $user, $qgeneral;
        $var = $this->variables;
        include $this->tpl_dir . '/Compiled/' . $tpl_name . '.' . $ext . '.php';
    }
	
	public static function compileAllFiles( $tpl_dir ){
		if($file = glob( $tpl_dir . '/Compiled/*' ))
			foreach( $file as $delfile)
				unlink($delfile);
				
		if ($handle = opendir($tpl_dir)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != ".." && $file != "Compiled")
					template::compileTPL($tpl_dir, $file);
			} 
		}
		closedir($handle);	
	}
	
	public static function compileFile( $tpl_dir, $tpl_name, $ext){
		if($file = glob( $tpl_dir . '/Compiled/' . $tpl_name . $ext . '*.php' ))
			foreach( $file as $delfile)
				unlink($delfile);
				
		template::compileTPL($tpl_dir, $tpl_name.$ext);
	}
}

?>